package cn.edu.zut.cs;

import cn.edu.zut.cs.DAO.SetpriceDAO;
import cn.edu.zut.cs.DAO.UserDAO;
import cn.edu.zut.cs.DAO.LotinfoDAO;
import cn.edu.zut.cs.DAO.ParkingDAO;
import cn.edu.zut.cs.DAO.CarinfoDAO;
import cn.edu.zut.cs.Model.Setprice;
import cn.edu.zut.cs.Model.User;
import cn.edu.zut.cs.Model.Carinfo;
import cn.edu.zut.cs.Model.Parking;
import cn.edu.zut.cs.Model.Lotinfo;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.management.relation.Role;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
public class ParkingController {
    @Autowired
    UserDAO userDAO;
    @Autowired
    SetpriceDAO setpriceDAO;
    @Autowired
    LotinfoDAO lotinfoDAO;
    @Autowired
    CarinfoDAO carinfoDAO;
    @Autowired
    ParkingDAO parkingDAO;
    @Autowired
    User user;
    @Autowired
    Setprice setprice;
    @Autowired
    Lotinfo lotinfo;
    @Autowired
    Carinfo carinfo;
    @Autowired
    Parking parking;

    /*页面实现*/
    @RequestMapping(value = "/index01.html")
    public String index(){
        return "index01";
    }
    @RequestMapping(value = "/register.html")
    public String register(){
        return "register";
    }
    @RequestMapping(value = "/login.html")
    public String longin(){
        return "login";
    }
    @RequestMapping(value = "/menu.html")
    public String menu(){
        return "menu";
    }
    @RequestMapping(value = "/price.html")
    public String price(){
        return "price";
    }
    @RequestMapping(value = "/Mmenu.html")
    public String Mmenu(){
        return "Mmenu";
    }
    @RequestMapping(value = "/addlot.html")
    public String addlot(HttpServletRequest request){
        request.getSession().setAttribute("lotinfos",lotinfoDAO.queryAll());
        return "addlot";
    }
    @RequestMapping(value = "/showuser.html")   /*用户信息展示*/
    public String showuser(HttpServletRequest request){
        request.getSession().setAttribute("users",userDAO.queryAll());
        return "showuser";
    }
    @RequestMapping(value = "/bookpay.html")
    public String bookpay(){
        return "bookpay";
    }
    @RequestMapping(value = "/book.html")   /*车位信息展示*/
    public String book(HttpServletRequest request){
        request.getSession().setAttribute("lotinfos",lotinfoDAO.queryAll());
        return "book";
    }
    @RequestMapping(value = "/in.html")
    public String in(HttpServletRequest request){
        request.getSession().setAttribute("lotinfos",lotinfoDAO.queryAll());
        return "in";
    }
    @RequestMapping(value = "/self.html")
    public String self(HttpServletRequest request){
        String UserName=(String) request.getSession().getAttribute("UserName");
        request.getSession().setAttribute("user",userDAO.queryOneUser(UserName));
        return "self";
    }
    @RequestMapping(value = "/Mself.html")
    public String Mself(HttpServletRequest request){
        String UserName=(String) request.getSession().getAttribute("UserName");
        request.getSession().setAttribute("user",userDAO.queryOneUser(UserName));
        return "Mself";
    }
    @RequestMapping(value = "/VIPpay.html")
    public String VIPpay(){
        return "VIPpay";
    }
    @RequestMapping(value = "/addVIPuser.html")
    public String addVIPuser(HttpServletRequest request){
        request.getSession().setAttribute("lotinfos",lotinfoDAO.queryAll());
        return "addVIPuser";
    }
    @RequestMapping(value = "/VIPmenu.html")
    public String VIPmenu(){
        return "VIPmenu";
    }
    @RequestMapping(value = "/VIP2pay.html")
    public String VIP2pay(){
        return "VIP2pay";
    }
    @RequestMapping(value = "/VIPself.html")
    public String VIPself(HttpServletRequest request){
        String UserName=(String) request.getSession().getAttribute("UserName");
        request.getSession().setAttribute("user",userDAO.queryOneUser(UserName));
        return "VIPself";
    }
    @RequestMapping(value = "/VIPcar.html")
    public String VIPcar(){
        return "VIPcar";
    }
    @RequestMapping(value = "/out.html")
    public String out(){
        return "out";
    }
    @RequestMapping(value = "/outpay.html")
    public String outpay(HttpServletRequest request){
        String UserName=(String) request.getSession().getAttribute("UserName");
        int UserID=userDAO.queryUserID(UserName);
        String CarID=carinfoDAO.queryCarID1(UserID);
        request.getSession().setAttribute("parking",parkingDAO.queryOneParking(CarID));
        return "outpay";
    }
    @RequestMapping(value = "/VIPout.html")
    public String VIPout(){
        return "VIPout";
    }
    @RequestMapping(value = "/VIPoutpay.html")
    public String VIPoutpay(HttpServletRequest request){
        String UserName=(String) request.getSession().getAttribute("UserName");
        int UserID=userDAO.queryUserID(UserName);
        String CarID=carinfoDAO.queryCarID1(UserID);
        request.getSession().setAttribute("parking",parkingDAO.queryOneParking(CarID));
        return "VIPoutpay";
    }
    @RequestMapping(value = "/full.html")
    public String full(){
        return "full";
    }
    @RequestMapping(value = "/addVIPtime.html")
    public String addVIPtime(){
        return "addVIPtime";
    }
    @RequestMapping(value = "/minusVIPuser.html")
    public String minusVIPuser(HttpServletRequest request){
        request.getSession().setAttribute("VIPusers",userDAO.queryAllVIP());
        return "minusVIPuser";
    }
    @RequestMapping(value = "/minusbook.html")
    public String minusbook(HttpServletRequest request){
        request.getSession().setAttribute("Bookusers",userDAO.queryAllBook());
        return "minusbook";
    }
    private static final Log logger= LogFactory.getLog(ParkingController.class);

    /*注册功能*/
    @RequestMapping(value = "/register1.html")
    public String register1(HttpServletRequest request, Model model){
        String UserName=request.getParameter("UserName");//获取网页输入的内容
        String PassWord=request.getParameter("PassWord");
        int PhoneNum= Integer.parseInt(request.getParameter("PhoneNum"));
        if(UserName.equals(userDAO.queryUserName(UserName))==true)
        {
            logger.info("注册失败");
            model.addAttribute("messgeError","账号已存在，这注册失败");
            return "register";
        }
        else
            {
                user.setUserName(UserName);
                user.setPassWord(PassWord);
                user.setPhoneNum(PhoneNum);
                userDAO.insertUser(user);
                model.addAttribute("message","注册成功");
                return "login";
            }
    }
    /*登录功能*/    //提示框界面未完成
    @RequestMapping(value = "/login1.html")
    public String login1(HttpServletRequest request, Model model,User user){

        String UserName=request.getParameter("UserName");
        String PassWord=request.getParameter("PassWord");
        int UserID=userDAO.queryUserID(UserName);

        if(PassWord.equals(userDAO.queryPassWord(UserName))==true)  //判断密码
        {
            if(userDAO.queryRole(UserName).equals("普通用户")==true)    //判断身份
            {
                if(carinfoDAO.countCarinfo()-lotinfoDAO.countISVIPLot()<500)
                {
                    logger.info("登录成功");
                    model.addAttribute("message","登录成功");
                    request.getSession().setAttribute("UserName",UserName);
                    request.getSession().setAttribute("UserID",UserID);
                    return "menu";
                }
                else
                {
                    logger.info("停止服务");
                    model.addAttribute("message","停止服务");
                    return "full";
                }
            }
            else if(userDAO.queryRole(UserName).equals("管理员")==true)
            {
                logger.info("登录成功");
                model.addAttribute("message","管理员登录成功");
                request.getSession().setAttribute("UserName",UserName);
                request.getSession().setAttribute("UserID",UserID);

                return "Mmenu";
            }
            else    //还没实现VIP页面
            {
                logger.info("登录成功");
                model.addAttribute("message","VIP用户登录成功");
                request.getSession().setAttribute("UserName",UserName);
                request.getSession().setAttribute("UserID",UserID);
                return "VIPmenu";
            }
        }
        else
        {
            logger.info("登录失败");
            model.addAttribute("messageError","请重新输入");
            return "login";
        }

    }
    /*设定价格*/
    @RequestMapping(value = "/price1.html")
    public String price1(HttpServletRequest request, Model model){
        int Price= Integer.parseInt(request.getParameter("Price"));
        int YearPrice= Integer.parseInt(request.getParameter("YearPrice"));
        if(setpriceDAO.querySetprice()!=null)   //价格表已有价格记录，删除以后再插入
        {
            setpriceDAO.deleteSetprice();
            setprice.setPrice(Price);
            setprice.setYearPrice(YearPrice);
            setpriceDAO.insertSetprice(setprice);
            logger.info("价格修改成功");
            model.addAttribute("message","价格修改成功");
            return "Mmenu";
        }
        else    //价格表没有记录，直接插入
        {
            setprice.setPrice(Price);
            setprice.setYearPrice(YearPrice);
            setpriceDAO.insertSetprice(setprice);
            logger.info("价格修改成功");
            model.addAttribute("message","价格修改成功");
            return "Mmenu";
        }
    }
    /*增加车位*/
    @RequestMapping(value = "/addlot1.html")
    public String addlot1(HttpServletRequest request, Model model){
        int LotID= Integer.parseInt(request.getParameter("LotID"));
        if(lotinfoDAO.queryLotID(LotID)!=null){
            logger.info("车位增加失败");
            model.addAttribute("messgeError","车位已存在");
            return "addlot";
        }
        else
        {
            lotinfo.setLotID(LotID);
            lotinfoDAO.insertLotinfo(lotinfo);
            logger.info("增加成功");
            model.addAttribute("message","增加成功");
            return "Mmenu";
        }
    }
    /*VIP用户填写车信息+入场*/
    @RequestMapping(value = "/VIPcar1.html")
    public String VIPcar1(HttpServletRequest request, Model model){
        String CarID=request.getParameter("CarID");
        String CarClass=request.getParameter("CarClass");
        String UserName=(String) request.getSession().getAttribute("UserName");
        int UserID=userDAO.queryUserID(UserName);
        int LotID=userDAO.queryVIPLotID(UserName);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  //获取当前时间，到秒
        String ArriveTime= format.format(new Date());
        if(CarID.equals(carinfoDAO.queryCarID(CarID))==true)    //在车辆表中查找该车牌号
        {
            logger.info("该车已经在停车场中");
            model.addAttribute("messgeError","该车已存在");
            return "VIPcar";
        }
        else
        {
            if(lotinfoDAO.queryIsEmpty(LotID)==1&&lotinfoDAO.queryIsEnable(LotID)==1)
            {
                //填车辆表信息
                carinfo.setCarID(CarID);
                carinfo.setCarClass(CarClass);
                carinfo.setUserID(UserID);
                carinfoDAO.insertCarinfo(carinfo);
                //填停车表信息
                parking.setLotID(LotID);
                parking.setCarID(CarID);
                parking.setArriveTime(ArriveTime);
                parkingDAO.insertInParking(parking);
                //添加车辆表中车位信息
                carinfo.setCarID(CarID);
                carinfo.setLotID(LotID);
                carinfoDAO.modifyIn(carinfo);
                lotinfoDAO.modifyIn(LotID); //车位修改为已占用
                logger.info("VIP入场成功");
                model.addAttribute("messge","VIP入场成功");
                return "VIPmenu";
            }
            else
            {
                logger.info("该VIP车位已经使用");
                model.addAttribute("messgeError","该VIP车位已经使用");
                return "VIPcar";
            }
        }
    }
    /*预约车位*/
    @RequestMapping(value = "/book1.html")
    public String book1(HttpServletRequest request, Model model){
        String UserName=(String) request.getSession().getAttribute("UserName"); //把用户名去掉了
        int BookNum= Integer.parseInt(request.getParameter("BookNum"));
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        if(lotinfoDAO.queryISVIPLot(BookNum)==0&&lotinfoDAO.queryIsEmpty(BookNum)==1&&lotinfoDAO.queryIsEnable(BookNum)==1&&userDAO.queryBookNum(UserName)==0)    //不能是VIP车位，已占用车位
        {
            user.setBookNum(BookNum);
            user.setUserName(UserName);
            userDAO.modifyBookNum(user);
            lotinfoDAO.modifyPredetermine(BookNum);
            String BookTime=df.format(new Date());  //插入预定时间
            user.setBookTime(BookTime);
            userDAO.insertBookTime(user);

            model.addAttribute("messge","预定成功");
            return "menu";
        }
        else
        {
            logger.info("预约失败");
            model.addAttribute("messgeError","该车位被占用");
            return "book";
        }
    }
    /*普通用户填写车辆信息+入场*/
    @RequestMapping(value = "/in1.html")
    public String in1(HttpServletRequest request, Model model){
        //每个用户只能控制自己的车
        String CarID=request.getParameter("CarID");
        String CarClass=request.getParameter("CarClass");
        int LotID= Integer.parseInt(request.getParameter("LotID"));
        String UserName=(String) request.getSession().getAttribute("UserName"); //用来获取预定号 //获取登陆时填写的Session
        int UserID=userDAO.queryUserID(UserName);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  //获取当前时间，到秒
        String ArriveTime= format.format(new Date());
        if(CarID.equals(carinfoDAO.queryCarID(CarID))==true)    //在车辆表中查找该车牌号
        {
            logger.info("该车已经在停车场中");
            model.addAttribute("messgeError","该车已经在停车场中");
            return "in";
        }
        else    //若该车不在停车场中
        {
            if(userDAO.queryBookNum(UserName)==0)   //判断是否是预定用户
            {
                //车位条件判断  //不需要再进行车辆是否存在判断了
                if(lotinfoDAO.queryISVIPLot(LotID)==0&&lotinfoDAO.queryIsEmpty(LotID)==1&&lotinfoDAO.queryIsEnable(LotID)==1)
                {
                    //车辆表
                    carinfo.setCarID(CarID);
                    carinfo.setCarClass(CarClass);
                    carinfo.setUserID(UserID);
                    carinfoDAO.insertCarinfo(carinfo);
                    //停车表
                    parking.setLotID(LotID);
                    parking.setCarID(CarID);
                    parking.setArriveTime(ArriveTime);
                    parkingDAO.insertInParking(parking);
                    lotinfoDAO.modifyIn(LotID); //车位修改为已占用
                    //入场时间，用来给出场时更新记录
                    request.getSession().setAttribute("ArriveTime",ArriveTime);
                    //添加车辆表中车位信息
                    carinfo.setCarID(CarID);
                    carinfo.setLotID(LotID);
                    carinfoDAO.modifyIn(carinfo);   //修改车辆表中车位信息
                    logger.info("普通用户入场成功");
                    model.addAttribute("messge","普通用户入场成功");
                    return "menu";
                }
                else
                {
                    logger.info("车位已占用或者是VIP车位");
                    model.addAttribute("messgeError","车位已占用或者是VIP车位");
                    return "in";
                }
            }
            else    //预定用户的入场
            {
                //预约过的车位可以直接入场
                if(LotID==userDAO.queryBookNum(UserName))    //判断输入的车位是否是预定的车位号（BookNum）
                {
                    //车辆表
                    carinfo.setCarID(CarID);
                    carinfo.setCarClass(CarClass);
                    carinfo.setUserID(UserID);
                    carinfoDAO.insertCarinfo(carinfo);
                    //停车表
                    parking.setLotID(LotID);
                    parking.setCarID(CarID);
                    parking.setArriveTime(ArriveTime);
                    parkingDAO.insertInParking(parking);
                    lotinfoDAO.modifyIn(LotID); //车位修改为已占用
                    //添加车辆表中车位信息
                    carinfo.setCarID(CarID);
                    carinfo.setLotID(LotID);
                    carinfoDAO.modifyIn(carinfo);   //修改车辆表中车位信息
                    //用户表
                    userDAO.modifyBookTime(UserName);   //按时到达的预约用户
                    logger.info("预定用户入场成功");
                    model.addAttribute("messge","预定用户入场成功");
                    return "menu";
                }
                else
                {
                    logger.info("输入的不是预定的车位号");
                    model.addAttribute("messgeError","输入的不是预定的车位号");
                    return "in";
                }
            }
        }
    }
    /*升级VIP功能*/
    @RequestMapping(value = "/addVIPuser1.html")
    public String addVIPuser1(HttpServletRequest request, Model model){
        String UserName=(String)request.getSession().getAttribute("UserName");
        int LotID= Integer.parseInt(request.getParameter("LotID"));
        String Role="VIP用户";
        int UserID=userDAO.queryUserID(UserName);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  //获取当前时间，到秒
        Calendar calendar=Calendar.getInstance();
        calendar.add(Calendar.YEAR, 1);
        String VIPTime=format.format(calendar.getTime());
        if(userDAO.queryRole(UserName).equals("普通用户")==true)
        {
            //判断是否用普通用户身份入过场
            if(carinfoDAO.queryUserID(UserID)==null)
            {
                //已预定的用户不能直接升级VIP
                if(userDAO.queryBookNum(UserName)==0)
                {
                    //输入的车位可用、为空，没有被预定
                    if(lotinfoDAO.queryIsEnable(LotID)==1&&lotinfoDAO.queryIsEmpty(LotID)==1&&lotinfoDAO.queryPredetermine(LotID)==0)
                    {
                        if(userDAO.queryVIPLotID(UserName)==0)
                        {
                            user.setUserName(UserName);
                            user.setVIPLotID(LotID);
                            user.setRole(Role);
                            user.setVIPTime(VIPTime);
                            userDAO.modifyVIPLotID(user);   //修改用户表中的身份和 VIPLotID
                            lotinfoDAO.modifyISVIPLot(LotID);   //修改车位表
                            logger.info("用户升级VIP成功");
                            model.addAttribute("message","用户升级VIP成功");
                            return "VIPmenu";
                        }
                        else
                        {
                            logger.info("该车位已经是VIP车位");
                            model.addAttribute("message","该车位已经是VIP车位");
                            return "addVIPuser";
                        }
                    }
                    else
                    {
                        logger.info("该车位已经被占用");
                        model.addAttribute("message","该车位已经被占用");
                        return "addVIPuser";
                    }
                }
                else
                {
                    logger.info("你已经有了预定车位，请先入场、出场");
                    model.addAttribute("message","你已经有了预定车位，请先入场、出场");
                    return "addVIPuser";
                }
            }
            else
            {
                logger.info("你已经停在普通用户车位，请先出场");
                model.addAttribute("message","你已经停在普通用户车位，请先出场");
                return "addVIPuser";
            }
        }
        else
        {
            logger.info("VIP续费");
            model.addAttribute("messgeError","VIP续费");
            return "addVIPuser";
        }
    }
    /*普通用户出场*/
    @RequestMapping(value = "/out1.html")
    public String out1(HttpServletRequest request, Model model) throws ParseException {
        String UserName=(String) request.getSession().getAttribute("UserName");
        int UserID=userDAO.queryUserID(UserName);
        if(carinfoDAO.queryUserID(UserID)!=null)    //判断是否入场了
        {
            String CarID=carinfoDAO.queryCarID1(UserID);
            int LotID=carinfoDAO.queryLotID(CarID);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  //获取当前时间，到秒
            String LeaveTime= format.format(new Date());
            String ArriveTime=parkingDAO.queryArriveTime(CarID);
            if(lotinfoDAO.queryPredetermine(LotID)==1)    //判断是否为预定用户
            {
                //计算时间
                Date ATime=format.parse(ArriveTime);
                Date LTime=format.parse(LeaveTime);
                long hour=(LTime.getTime()-ATime.getTime())/1000/60/60;
                int minus=(int)hour+1;
                int fullpay=minus*setpriceDAO.querySetprice();   //全款价格
                float pay= (float) (fullpay*0.7);
                parking.setLeaveTime(LeaveTime);
                parking.setPay(pay);
                parking.setCarID(CarID);
                parkingDAO.insertOutParking(parking);
                logger.info("预定用户出场成功");
                model.addAttribute("message","预定用户出场成功");
                request.getSession().setAttribute("parking",parkingDAO.queryOneParking(CarID));
                carinfoDAO.deleteCarinfo(CarID);    //删除车辆表中离开车辆信息
                lotinfoDAO.modifyOut(LotID);    //出场后，该车位恢复可用
                userDAO.modifyOutBookNum(UserName); //出场后，预定车位信息置空
                parkingDAO.deleteParking(CarID);    //删除停车表信息
                return "outpay";
            }
            else
            {
                Date ATime=format.parse(ArriveTime);
                Date LTime=format.parse(LeaveTime);
                long hour=(LTime.getTime()-ATime.getTime())/1000/60/60;
                int minus=(int)hour+1;
                float pay=minus*setpriceDAO.querySetprice();
                parking.setLeaveTime(LeaveTime);
                parking.setPay(pay);
                parking.setCarID(CarID);
                parkingDAO.insertOutParking(parking);
                logger.info("非预定用户出场成功");
                model.addAttribute("message","非预定用户出场成功");
                request.getSession().setAttribute("parking",parkingDAO.queryOneParking(CarID));
                carinfoDAO.deleteCarinfo(CarID);    //删除车辆表中离开停车场车辆信息
                lotinfoDAO.modifyOut(LotID);    //出场后，该车位恢复可用
                parkingDAO.deleteParking(CarID);    //删除停车表信息
                return "outpay";
            }
        }
        else
        {
            logger.info("你的车还未入场");
            model.addAttribute("message","你的车还未入场");
            return "out";
        }

    }
    /*VIP用户出场*/
    @RequestMapping(value = "/VIPout1.html")
    public String VIPout1(HttpServletRequest request, Model model) throws ParseException {
        String UserName=(String) request.getSession().getAttribute("UserName");
        int UserID=userDAO.queryUserID(UserName);
        if(carinfoDAO.queryUserID(UserID)!=null)    //判断是否入过场
        {
            String CarID=carinfoDAO.queryCarID1(UserID);
            int LotID=carinfoDAO.queryLotID(CarID);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  //获取当前时间，到秒
            String LeaveTime= format.format(new Date());
            String ArriveTime=parkingDAO.queryArriveTime(CarID);
            Date ATime=format.parse(ArriveTime);
            Date LTime=format.parse(LeaveTime);
            long hour=(LTime.getTime()-ATime.getTime())/1000/60/60;
            int minus=(int)hour+1;
            float pay=minus*setpriceDAO.querySetprice();
            parking.setLeaveTime(LeaveTime);
            parking.setPay(pay);
            parking.setLotID(LotID);
            parkingDAO.insertOutParking(parking);
            logger.info("VIP用户出场成功");
            model.addAttribute("message","VIP用户出场成功");
            request.getSession().setAttribute("parking",parkingDAO.queryOneParking(CarID));
            carinfoDAO.deleteCarinfo(CarID);    //删除车辆表中离开停车场车辆信息
            parkingDAO.deleteParking(CarID);    //删除停车表信息
            lotinfoDAO.modifyOut(LotID);
            return "VIPoutpay";
        }
        else
        {
            logger.info("你的车还未入场");
            model.addAttribute("message","你的车还未入场");
            return "VIPout";
        }
    }
    /*VIP续费*/
    @RequestMapping(value = "/addVIPtime1.html")
    public String addVIPtime1(HttpServletRequest request, Model model) throws ParseException {
        String UserName=(String)request.getSession().getAttribute("UserName");
        String oldVIPTime=userDAO.queryVIPTime(UserName);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date oldTime=format.parse(oldVIPTime);
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(oldTime);
        rightNow.add(Calendar.YEAR, 1);// 日期加1年
        Date dt1 = rightNow.getTime();
        String VIPTime = format.format(dt1);
        user.setUserName(UserName);
        user.setVIPTime(VIPTime);
        userDAO.modifyVIPTime(user);
        return "VIPmenu";
    }
    /*清除过期VIP用户*/
    @RequestMapping(value = "/minusVIPuser1.html")
    public String minusVIPuser1(HttpServletRequest request, Model model) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  //获取当前时间，到秒
        List<User> list=userDAO.queryAll();
        for(int i=0;i<list.size();i++)
        {
            if(list.get(i).getRole().equals("VIP用户")==true)
            {
                String UserName=list.get(i).getUserName();
                String VIPTime=userDAO.queryVIPTime(UserName);  //到期时间
                String nowTime= df.format(new Date());  //现在时间
                Date VTime=df.parse(VIPTime);
                Date nTime=df.parse(nowTime);
                int flag=nTime.compareTo(VTime);
                if(flag==1)
                {
                    int VIPLotID=userDAO.queryVIPLotID(UserName);
                    lotinfoDAO.minusVIPuser(VIPLotID);  //清除车位表中该用户VIP车位
                    userDAO.minusVIPuser(UserName); //清除用户表中该用户VIP车位
                }
                else
                {
                    continue;
                }
            }
            else
            {
                continue;
            }
        }
        logger.info("清除过期VIP成功");
        model.addAttribute("message","清除过期VIP成功");
        return "Mmenu";
    }
    /*清除过期预定*/
    @RequestMapping(value = "/minusbook1.html")
    public String minusbook1(HttpServletRequest request, Model model) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  //获取当前时间，到秒
        List<User> list=userDAO.queryAll();
        for(int i=0;i<list.size();i++)
        {
            if(list.get(i).getBookTime()!=null)
            {

                String UserName=list.get(i).getUserName();
                String BookTime=userDAO.queryBookTime(UserName);
                int BookNum=list.get(i).getBookNum();
                String nowTime= df.format(new Date());  //现在时间
                Date nTime=df.parse(nowTime);
                Date oldTime=df.parse(BookTime);
                Calendar rightNow = Calendar.getInstance();
                rightNow.setTime(oldTime);
                rightNow.add(Calendar.HOUR, 2);// 日期加2小时
                Date dt1 = rightNow.getTime();
                String BTime = df.format(dt1);
                Date endTime=df.parse(BTime);
                int flag=nTime.compareTo(endTime);
                if(flag==1)
                {
                    userDAO.minusBook(UserName);
                    lotinfoDAO.minusBook(BookNum);
                }
                else
                {
                    continue;
                }
            }
            else
            {
                continue;
            }
        }
        logger.info("清除过期预定成功");
        model.addAttribute("message","清除过期预定成功");
        return "Mmenu";
    }
}
